<template>
    <div>
        <el-card>
          <h2>欢迎登录 - MMALL管理系统</h2>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
                <el-form-item  prop="name" >
                    <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item  prop="pwd">
                    <el-input v-model="ruleForm.pwd"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>
<script>
import { login} from "@/utils/API";
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        pwd: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        pwd: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 1, max: 6, message: "长度在 1 到 6 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          login({
            username: this.ruleForm.name,
            password: this.ruleForm.pwd
          }).then(res => {
            console.log(res);
            localStorage.setItem("username", res.username);
            this.$router.push({ path: "/Index" });
          });
        } else {
          alert("校验错误");
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.el-card{
    width: 460px;
    // margin: 150px auto;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%)
}
::v-deep(.el-form-item__content){
  margin-left: 0 !important;
}
.el-button{
  width: 100%;
  height: 50px;
}
</style>
